package com.xiaoxin.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxin.common.utils.uuid.IdUtils;
import com.xiaoxin.framework.aspectj.lang.annotation.DataSource;
import com.xiaoxin.framework.aspectj.lang.enums.DataSourceType;
import com.xiaoxin.framework.web.controller.BaseController;
import com.xiaoxin.framework.web.domain.AjaxResult;
import com.xiaoxin.framework.web.page.TableDataInfo;
import com.xiaoxin.project.system.domain.SysConfig;
import com.xiaoxin.project.system.domain.SysOrder;
import com.xiaoxin.project.system.service.SysConfigService;
import com.xiaoxin.project.system.service.SysOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单 Controller
 *
 * @author ZhangXX
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关api")
public class SysOrderController extends BaseController {
    @Autowired
    private SysOrderService sysOrderService;

    @GetMapping("/add/{userId}")
    @ApiOperation("添加订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long", paramType = "path")
    })
    public AjaxResult add(@PathVariable("userId") Long userId) {
        SysOrder sysOrder = new SysOrder();
        sysOrder.setUserId(userId);
        sysOrder.setStatus("0");
        sysOrder.setOrderNo(IdUtils.fastSimpleUUID());
        return AjaxResult.success(sysOrderService.insertSysOrder(sysOrder));
    }

    @ApiOperation("查询订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "Long")
    })
    @GetMapping("/list")
    public TableDataInfo list(SysOrder sysOrder) {
        //hintSharding();
        startPage();
        List<SysOrder> sysOrders = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(sysOrders);
    }

    @ApiOperation("根据订单id查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "Long", paramType = "path")
    })
    @GetMapping("/query/{orderId}")
    public AjaxResult query(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(sysOrderService.selectSysOrderById(orderId));
    }

    @ApiOperation("根据用户id查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startUserId", value = "开始用户id", dataType = "Long"),
            @ApiImplicitParam(name = "endUserId", value = "结束用户id", dataType = "Long")
    })
    @GetMapping("/query/order")
    public AjaxResult query(@RequestParam("startUserId") Long startUserId,
                            @RequestParam("endUserId") Long endUserId) {
        List<SysOrder> sysOrders =sysOrderService.selectSysOrderByUserId(startUserId,endUserId);
        return AjaxResult.success(sysOrders);
    }

    @Autowired
    private SysConfigService sysConfigService;

    @ApiOperation("查询系统配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "Long")
    })
    @GetMapping("/configlist")
    @DataSource(DataSourceType.SHARDING)
    public TableDataInfo list(SysConfig sysConfig) {
        startPage();
        List<SysConfig> sysOrders = sysConfigService.list(new QueryWrapper<>(sysConfig));
        return getDataTable(sysOrders);
    }

    private void hintSharding() {
        // 清除掉上一次的规则，否则会报错
        HintManager.clear();
        // HintManager API 工具类实例
        HintManager hintManager = HintManager.getInstance();
        // 直接指定对应具体的数据库
        hintManager.addDatabaseShardingValue("order", 1);
        // 设置表的分片健
        hintManager.addTableShardingValue("sys_order", "_0");
        hintManager.addTableShardingValue("sys_order", "_1");

        // 在读写分离数据库中，Hint 可以强制读主库
        //hintManager.setMasterRouteOnly();
    }


}
